package Donnees;

import java.util.ArrayList;
import java.util.List;

public class Facade
{
	private static Facade instance;
	
	private Facade() {}
	
	public static Facade getInstance(){
		if (instance == null) {
			instance = new Facade();
		}
		return instance;
	}
	
	public SpectacleBean[] cmdRechercheSpectacles()
	{
		List<SpectacleBean> spectacles = Catalogue.getInstance().getSpectacles();
		SpectacleBean[] array = spectacles.toArray(new SpectacleBean[spectacles.size()]);
		return array;
	}
	
	public SpectacleBean[] cmdRechercheSpectacles(int noPage, int maxlignes)
	{
		List<SpectacleBean> spectacles = Catalogue.getInstance().getSpectacles();	
		List<SpectacleBean> subList = spectacles.subList((noPage-1)*maxlignes, (noPage-1)*maxlignes + Math.min(maxlignes, spectacles.size()-((noPage-1)*maxlignes)));		
		SpectacleBean[] array = subList.toArray(new SpectacleBean[subList.size()]);
		return array;
	}
	
	public int cmdNbPagesSpectacles(int maxlignes)
	{
		List<SpectacleBean> spectacles = Catalogue.getInstance().getSpectacles();	
		double a = spectacles.size();
		double b = maxlignes;
		int nbPages = (int)((double) Math.ceil(a/b));
		return nbPages;	
	}
	
	public int cmdRechercheIndexSpectacle(int id)
	{
		for (int i = 0; i < Catalogue.getInstance().getSpectacles().size(); i++)
		{
			SpectacleBean current = Catalogue.getInstance().getSpectacles().get(i);
			if (id == current.getId())
				return i;
		}
		
		return 0;
	}

	public SpectacleBean cmdRechercheSpectacle(int index)
	{
		SpectacleBean spectacle = Catalogue.getInstance().getSpectacles().get(index);
		return spectacle;
	}
	
	public RepresentationBean[] cmdRechercheRepresentations(int index)
	{
		List<RepresentationBean> representations = Catalogue.getInstance().getSpectacles().get(index).getRepresentations();
		RepresentationBean[] array = representations.toArray(new RepresentationBean[representations.size()]);
		return array;
	}
	
	public SpectacleBean[] cmdRechercheRepresentations(String humoriste)
	{
		
		List<SpectacleBean> spectacles = Catalogue.getInstance().getSpectacles();
		List<SpectacleBean> array = new ArrayList<SpectacleBean>();
		SpectacleBean[] returnValue;
		
		for(int i = 0; i < spectacles.size(); i++)
		{
			if(spectacles.get(i).getArtiste().equals(humoriste))
			{
				array.add(spectacles.get(i));
			}
		}
		
		if(array.size() == 0)
			returnValue = null;
		else
			returnValue = array.toArray(new SpectacleBean[array.size()]);
		
		return returnValue;
	}
	
	public RepresentationBean[] cmdRechercheRepresentations(int index, int noPage, int maxlignes)
	{
		List<RepresentationBean> representations = Catalogue.getInstance().getSpectacles().get(index).getRepresentations();	
		List<RepresentationBean> subList = representations.subList((noPage-1)*maxlignes, (noPage-1)*maxlignes + Math.min(maxlignes, representations.size()-((noPage-1)*maxlignes)));		
		RepresentationBean[] array = subList.toArray(new RepresentationBean[subList.size()]);
		return array;
	}
	
	public int cmdNbPagesRepresentations(int index, int maxlignes)
	{
		List<RepresentationBean> representations = Catalogue.getInstance().getSpectacles().get(index).getRepresentations();	
		double a = representations.size();
		double b = maxlignes;
		int nbPages = (int)((double) Math.ceil(a/b));
		return nbPages;	
	}
	
	public int cmdRechercheIndexRepresentation(SpectacleBean spectacle, int id)
	{
		for (int i = 0; i < spectacle.getRepresentations().size(); i++)
		{
			RepresentationBean current = spectacle.getRepresentations().get(i);
			if (id == current.getId())
				return i;
		}
		
		return 0;
	}
	
	public RepresentationBean cmdRechercheRepresentation(SpectacleBean spectacle, int index)
	{
		RepresentationBean representation = spectacle.getRepresentations().get(index);
		return representation;
	}
}
